Cuestionario Ampliado del Censo de Población y Vivienda 2010

El cuestionario ampliado se guarda en un un archivo .RData.

data <- read_sav("~/Cuestionario Ampliado_2010_Persona.sav")

save(data, 
      file = paste0(here::here(), "/Bases/Censo_Personas_2010.RData"))

Se seleccionan las variables que se desean conservar para la realización de este documento y se guarda en un archivo .RData para practicidad del manejo de datos.

Posibles variables que se pueden contemplar en la migración reciente

  • EDAD
  • SEXO
  • AFRODES 
  • HLENGUA 
  • QDIALECT_C
  • LI_INALI
  • PERETN 
  • NIVACAD
  • ALFABET
  • ESTCON 
  • CONACT 
  • HIJOS_NAC_VIVOS

La variable mydata contiene 11 938 402 observaciones y 12 variables.

load(paste0(here::here(), "/Bases/Censo_Personas_2010.RData"))

mydata <- data %>%
           select(ENT, NOM_ENT, MUN, NOM_MUN, ENT_MUN, ENT_MUN_RES, RES05EDO_C, RES05PAI_C, MUN05OTR_C,
                  EDAD, SEXO, HLENGUA, QDIALECT_C, LI_INALI, PERETN, NIVACAD, ALFABET, 
                  ESTCON, CONACT, FACTOR, ESTRATO, UPM) %>%
            rename("CVE_ENT" = "ENT",
                   "CVE_MUN" = "ENT_MUN",
                   "CVE_MUN_RES" = "ENT_MUN_RES") %>%
             mutate(CVE_ENT = str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"))

save(mydata, file = paste0(here::here(), "/Bases/01_Migracion interna_2010.RData"))

✔️A partir de aquí se pueden correr los códidos 👇.

Se carga el archivo Migracion interna_2010.RData.

load(paste0(here::here(), "/Bases/01_Migracion interna_2010.RData"))

# Para fines prácticos se genera un ponderador de uno 
mydata <- mydata %>%
           select(CVE_ENT, NOM_ENT, CVE_MUN, NOM_MUN, RES05EDO_C, MUN05OTR_C, CVE_MUN_RES, EDAD, FACTOR, ESTRATO, UPM) %>%
            mutate(M = 1)  %>%
             mutate(NOM_ENT = as.factor(.$CVE_ENT)) %>%
              ungroup()

Claves de entidades y municipios

Se genera un vector con el nombre de las entdades llamado estados para facilitar los filtros en el documento. 
Se genera un vector con las abreviaturas de las entidades llamado est para fines prácticos.  
Se genera un vector con las claves de los municipios.

# Claves de los estados
estados <- sjlabelled::get_labels(mydata$CVE_ENT)

nom_estados <- c( "Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila de Zaragoza",
                  "Colima", "Chiapas", "Chihuahua", "Ciudad de México", "Durango", 
                  "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",  "México", 
                  "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", 
                  "Puebla", "Querétaro", "Quintana Roo", "San Luis Potosí", "Sinaloa", 
                  "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala","Veracruz de Ignacio de la Llave", 
                  "Yucatán", "Zacatecas")

est <- c("AGS", "BC", "BCS", "CAMP", "COAH", "COL", "CHIS", "CHIH", "CDMX", "DGO", "GTO", "GRO", "HGO",
         "JAL", "MEX", "MICH", "MOR", "NAY", "NL", "OAX", "PUE", "QRO", "QROO", "SLP","SIN","SON", "TAB", 
         "TAMS", "TLX", "VER", "YUC", "ZAC")

# Claves de los municipios
MUN <- readRDS(paste0(here::here(), "/Bases/municipios_2010.RDS"))
nom_municipios <- sjlabelled::get_labels(MUN$NOM_MUN) %>% as.factor()
municipios <- sjlabelled::get_labels(MUN$CVE_MUN) %>% as.factor()

# Se le asignan las etiquetas a los nombres de los estados 
levels(mydata$NOM_ENT) <- estados

Población de 5 años y más

Se identifica a la población de 5 años y más.

filter(EDAD >= 5 & EDAD != 999)'.

Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <= 130) %>%
                 filter(RES05EDO_C %in% estados)  # Filtro del lugar de residencia dentro del país. 

Nivel estatal

Migración reciente

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 5 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
       as.data.frame() %>%
        mutate(EDAD = as.numeric(.$EDAD)) %>%
         subset(EDAD >= 5 & EDAD <= 130) %>%
          filter(RES05EDO_C %in% estados) %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/MC_estado.RDS"))

Se genera una matriz cruzada del lugar de residencia hace 5 años a nivel estatal, utilizando la función svytable de la paquetería survey.

MC <- readRDS(paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/MC_estado.RDS")) 

Migrantes <- svytable(~RES05EDO_C + CVE_ENT, design = MC) 

La función cross_cases() de la paquetería expss se utiliza para crear tablas de contingencia cruzadas a partir de dos o más variables categóricas. Utilizando el comando weight, permite ponderar las observaciones “factores de expansión” en la tabla.

Se quita la diagonal a la matriz cruadrada con la función diag.remove() de la paquetería sna, donde esta función reemplaza los elementos de la diagonal principal de una matriz por un valor nulo o por el valor especifico.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_ENT, RES05EDO_C, weight = Freq) %>%
                as.data.frame() %>%
                 slice(-33) %>% 
                  select(-row_labels) 

rownames(Migrantes)<- nom_estados
colnames(Migrantes) <- nom_estados

save(Migrantes, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.RData"))

wb <- createWorkbook()
addWorksheet(wb, "MREst. 2005-2010")
writeData(wb, 1, Migrantes, colNames = TRUE, rowNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel estatal, 2005 - 2010

Matriz de migración reciente 2005 - 2010
Nivel estatal
Entidad Aguascalientes Baja California Baja California Sur Campeche Coahuila de Zaragoza Colima Chiapas Chihuahua Ciudad de México Durango Guanajuato Guerrero Hidalgo Jalisco México Michoacán de Ocampo Morelos Nayarit Nuevo León Oaxaca Puebla Querétaro Quintana Roo San Luis Potosí Sinaloa Sonora Tabasco Tamaulipas Tlaxcala Veracruz de Ignacio de la Llave Yucatán Zacatecas
Aguascalientes 989821 1212 70 11 722 182 696 1158 7061 374 2661 601 296 9199 7.5e+03 1044 461 271 907 638 717 748 154 1296 381 545 37 1059 430 518 16 7408
Baja California 2227 2589610 3431 149 956 1569 11791 3393 6732 2874 5486 5670 1816 12391 7.5e+03 10977 2431 5188 858 8842 6023 1207 338 581 31400 17409 1231 920 416 8552 184 1478
Baja California Sur 319 6148 482904 16 1062 855 2633 1456 6595 1235 1667 11203 678 6708 7.3e+03 1482 412 926 887 3385 4202 338 2099 444 11149 3251 209 595 524 2127 293 147
Campeche 33 267 18 698023 1032 3 2192 615 1807 84 290 225 43 370 5.3e+02 253 179 0 39 577 799 337 3999 210 164 334 9374 1415 154 4606 2778 64
Coahuila de Zaragoza 941 806 46 15 2380518 260 1650 12219 2994 10513 1607 221 498 1708 2.8e+03 221 205 204 13194 1239 918 1730 198 1119 844 820 908 3755 162 2584 122 2317
Colima 278 2473 205 198 254 528425 875 490 2156 239 949 2968 788 18810 1.9e+03 6530 45 328 407 595 315 420 323 585 312 948 372 566 100 1165 20 398
Chiapas 104 5097 414 2355 431 122 4142076 1895 6691 118 635 1483 346 3303 5.3e+03 891 862 586 1014 4190 2351 513 3133 159 983 578 5764 1350 155 4477 438 105
Chihuahua 403 2079 245 145 6974 306 2703 2937330 3211 10843 876 1179 501 2120 2.4e+03 1594 461 178 2032 2019 1721 528 152 514 2916 3526 386 1312 96 7321 107 2176
Ciudad de México 2162 3512 1705 986 1472 722 4746 5200 7753531 1323 6277 10672 10270 8613 1.6e+05 10541 9495 750 5247 15570 24350 4446 3798 1944 2981 2474 4196 3661 2058 19301 1752 1145
Durango 235 2453 205 5 9699 52 401 12693 516 1385550 395 70 78 1242 5.0e+02 227 488 199 1648 69 370 408 184 433 1838 642 14 410 12 710 84 2277
Guanajuato 2390 1721 498 268 2370 585 1980 1208 18502 814 4733141 1151 2025 9389 1.5e+04 10429 900 418 1343 932 2048 8764 482 5356 1016 711 765 2319 775 2876 536 872
Guerrero 219 2962 1496 139 307 786 519 1062 7760 158 721 2915573 404 2158 9.0e+03 5947 6712 229 226 2380 2189 467 1601 486 1497 1115 167 1093 227 1831 113 89
Hidalgo 474 1554 76 177 788 356 503 1032 34725 277 1900 1100 2238160 2317 5.6e+04 1666 1283 206 1690 963 6173 2297 443 1459 821 345 533 1541 1794 4733 189 212
Jalisco 6084 10937 2653 237 1148 7033 4512 3241 14002 1319 7905 4088 4261 6342992 1.3e+04 22648 1132 11687 3109 2422 2609 2292 1004 3844 8374 4270 630 2661 190 7103 478 4284
México 1682 7838 1157 735 1935 441 6564 3134 389452 1689 8519 17553 23916 9655 1.3e+07 15949 9781 855 3738 15980 26562 8486 3259 3306 2264 2587 2338 3371 4229 25667 1205 1326
Michoacán de Ocampo 570 6132 358 160 932 2206 991 913 15790 212 6934 8290 933 12054 1.4e+04 3734751 970 386 811 1101 1132 2116 335 756 587 1119 252 795 213 2433 310 212
Morelos 348 1284 165 100 332 58 613 483 27109 137 337 16134 1102 1281 1.6e+04 1276 1500899 193 388 1666 5807 839 173 379 312 222 296 450 238 4046 70 280
Nayarit 479 8732 934 80 267 620 2683 604 2343 859 589 3097 243 26639 2.4e+03 2828 94 884102 600 544 534 488 252 270 3277 1622 21 212 27 635 7 572
Nuevo León 665 2188 382 314 15487 329 1699 4688 7926 2376 2368 2179 6751 4418 7.0e+03 1093 961 330 3989017 4036 2134 1924 1494 23308 1901 955 2108 19178 217 17470 281 4532
Oaxaca 177 4570 990 298 494 440 5526 2023 14513 260 655 3113 631 1485 1.7e+04 1069 1069 161 933 3275659 6578 605 719 284 1779 1347 1230 1311 257 12888 331 287
Puebla 737 3393 1196 528 951 140 3325 1659 26416 216 1102 3917 4563 2384 2.5e+04 1454 5183 107 859 8564 5004644 1690 1436 364 436 1142 2298 1404 8732 19298 784 205
Querétaro 680 356 108 50 810 270 417 1711 26121 514 9416 1139 4435 3643 2.5e+04 4183 1840 352 1953 1078 1856 1522053 442 2446 315 918 847 1421 433 3180 178 822
Quintana Roo 516 973 489 8110 470 1095 18833 889 14805 467 761 2459 969 4039 8.2e+03 1181 1751 114 1264 2118 3306 1016 1023772 294 425 437 23333 1636 702 16623 22380 15
San Luis Potosí 899 754 192 4 2531 221 470 809 6208 714 3669 639 1101 2179 4.6e+03 1058 518 489 9612 769 767 1937 477 2233668 553 398 386 10279 87 2131 25 2545
Sinaloa 64 25120 2571 0 1172 377 500 2463 2095 3476 804 1780 1656 6787 1.6e+03 1716 216 2917 1778 1306 1038 172 94 823 2392883 11489 137 893 67 1507 55 539
Sonora 252 17168 1065 49 473 471 2110 6690 2254 879 446 1452 609 3206 1.6e+03 1022 178 1676 1218 2015 666 200 316 248 19447 2263186 372 394 63 2292 475 552
Tabasco 32 937 34 4964 950 318 12886 673 4132 32 394 429 600 1230 2.2e+03 575 470 0 1227 1118 2219 256 5550 112 552 131 1940421 2453 110 11226 2001 18
Tamaulipas 900 1067 78 605 3568 48 1397 1880 6493 1641 2488 1007 3854 2025 5.1e+03 2159 552 469 13757 1729 2101 612 647 11565 1137 375 1900 2748980 97 48093 106 531
Tlaxcala 122 638 45 84 259 59 384 351 8539 74 374 486 1506 607 7.8e+03 710 757 15 215 596 13365 288 198 75 146 255 444 243 1011697 1883 54 124
Veracruz de Ignacio de la Llave 318 6889 919 3354 3546 786 5429 12868 30269 767 2425 2345 4246 5037 2.7e+04 2260 2130 312 7736 14782 22207 2006 6014 3030 2165 2679 8703 30444 2198 6665740 1325 731
Yucatán 325 489 304 7022 184 7 3115 879 3963 7 193 138 346 685 4.5e+03 913 39 7 575 556 822 476 17984 93 98 471 3328 334 497 3529 1707091 25
Zacatecas 3531 989 305 51 1872 74 184 3112 1690 1650 1042 122 351 5096 9.6e+02 439 136 232 1744 76 353 260 115 1474 409 285 63 698 0 483 80 1275473
Fuente: Estimaciones del CONAPO.

Gráfico dinámico

Gráfico dinámico de migración reciente a nivel estatal.

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

names <- c("Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila", "Colima", 
           "Chiapas", "Chihuahua", "Ciudad de México", "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",    
           "México", "Michoacán", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", "Querétaro", 
           "Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala", 
           "Veracruz", "Yucatán", "Zacatecas")

# Paleta de colores
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

p <- chorddiag(tabla, 
               groupNames = names,
               groupColors = paleta, 
               groupnamePadding = 10, 
               #height = 700, 
               #width = 700,
               margin = 120,
               groupThickness = 0.07,
               groupPadding = 3,
               groupnameFontsize = 12,
               fadeLevel = '0.1',
               tickInterval = seq(0, 500000, 10000),
               chordedgeColor = "transparent",
               showZeroTooltips = FALSE,
               showTicks = TRUE)

# Ajusta las etiquetas usando JavaScript para modificar su posición
p <- htmlwidgets::onRender(p, '
      function(el, x) {
        d3.selectAll(".group text")
          .attr("text-anchor", "middle")
          .attr("dx", "0")  
          .attr("dy", "0.75em"); 
      }
')

# Crear un contenedor div y aplicar estilos CSS para centrarlo
#p <- tags$div(style = "display: flex; justify-content: center; align-items: center;", p)

p
p %>% 
 mapview::mapshot(url = paste0(here::here(),"/images/MR5a_2010.html"))

#htmlwidgets::saveWidget(p, paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2010/MR5a a nivel estatal 2005 - 2010.html"), selfcontained = TRUE)
#require(webshot)
#webshot(url = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2010/MR5a a nivel estatal 2005 - 2010.html"),
 #         file = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2010/MR5a a nivel estatal 2005 - 2010.png"),
  #                cliprect = "viewport")

Gráficos migración reciente

ChordDiagram

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0)  

rownames(tabla) <- stringr::str_wrap(nom_estados, 50)
colnames(tabla) <- stringr::str_wrap(nom_estados, 50)

# Paleta de colores
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla, paleta)
#svglite::svglite(paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2010/ChordDiagram de MR5a a nivel estatal.svg"), width = 20, height = 20)
file = "/Graficos/Estado/01_Migracion reciente/2010/ChordDiagram de MR5a a nivel estatal.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 7, 
                    height = 7, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0.2,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

rownames(tabla) <- stringr::str_wrap(nom_estados, 100)
colnames(tabla) <- stringr::str_wrap(nom_estados, 100)

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) 

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#tabla_estados <- sapply(1:32, function(x)
 #                       mean(tail(sort(tabla1[[x]]), 1), na.rm = TRUE))
#p <- data.frame(estados = est,
 #               filtro_estados = filtro_mig)
#write.table(p, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Filtro a nivel estatal.txt"))
#write.xlsx(p, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Filtro a nivel estatal.xlsx"))

filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Filtro a nivel estatal.xlsx"), colNames = TRUE) %>% 
               pull(filtro_estados)

tabla1 <- migration_flows_states(tabla = tabla, 
                                 filtro_mig = filtro_mig, 
                                 filtro_est = filtro_est, 
                                 category = estado, 
                                 group = "Otro estados")

## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
                                 name = "Total",,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/Matriz MR5a por estados_2005-2010_Reduccion.xlsx"), 
               overwrite = TRUE)
}
saveRDS(tabla1, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/Tabla MR5a a por estados.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/Tabla MR5a a por estados.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_ENT", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_ENT", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/Matriz MR5a por estados_2005-2010_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/Tabla MR5a a por estados.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Estado/01_Migracion reciente/2010/ChordDiagram de MR5a desagregado por estado.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 8, 
                    height = 8, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Estado/01_Migracion reciente/2010/Etiquetas a nivel estatal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.RData"))

Migrantes <- Migrantes %>%
              sna::diag.remove(remove.val = 0)  

rownames(Migrantes) <- stringr::str_wrap(nom_estados, 50)
colnames(Migrantes) <- stringr::str_wrap(nom_estados, 50)

# Matiz migración interna (Población de 5 años y más)
tabla <- Migrantes %>% 
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             as_tibble() %>%
              mutate(rn = forcats::fct_relevel(.$rn, nom_estados),
                     cn = forcats::fct_relevel(.$cn, nom_estados)) %>%
               filter(value >= 0)  
p <- tabla %>% 
       ggplot(aes(axis1 = rn, 
                   axis2 = cn, 
                    y = value),  # c("value", "freq", "tasa")
               reverse = FALSE, 
                na.rm = TRUE) +
        geom_alluvium(aes(fill = rn),
                       curve_type = "quintic", 
                        color = "transparent", 
                         alpha = 0.85, 
                          lwd = 0.001, 
                           width = 1/5,
                            reverse = FALSE) +
         geom_stratum(aes(fill = cn), 
                       color = "white", 
                        alpha = 0.65,  
                         lwd = 0.001, 
                          width = 1/5,
                           reverse = FALSE) +
          geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                               fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                           stat = "stratum", 
                            size = 3, 
                             direction = "y", 
                              nudge_x = -.3,
                               min.segment.length = unit(1, "lines"),
                                force = 1,
                                 force_pull = 0,
                                  family = "montserrat",
                                   reverse = FALSE) +
           geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                               fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                            stat = "stratum", 
                             size = 3,
                              direction = "y", 
                               nudge_x = .3, 
                                force = 1,
                                 force_pull = 0,
                                  family = "montserrat",
                                   reverse = FALSE) +
            theme_void() + 
             theme(plot.margin = margin(t = 1, r = 1, b = 1, l = 1, "cm"),
                    text = element_text(family = "montserrat"),
                     axis.text = element_blank(),
                      axis.title = element_blank(),
                       strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                        legend.key.size = unit(0.5, "cm"),
                         legend.text = element_text(size = 9, family = "montserrat")) + 
              scale_x_discrete(expand = c(0, 0.4)) +
               scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                 labs(fill = "", 
                       color = "")
p
path = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2010/GSankey de MR5a a nivel estatal.pdf")
ggexport(p, width = 14, height = 10, dpi = 400, filename = path)

Desagregado por estados

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.RData"))

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) %>%
           as.data.frame() %>%
            tibble::rownames_to_column(var = "rn") %>% 
             melt(., id.vars = "rn", variable.name = "cn") %>%
              mutate_if(is.factor, as.character) %>%
               mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% estados | .$cn %in% estados), value, 0)) %>%
                mutate(rn = forcats::fct_relevel(.$rn, estados),
                       cn = forcats::fct_relevel(.$cn, estados)) 
p <- lapply(1:32, function(x){
                   tabla <- tabla %>%
                             mutate(rn = forcats::fct_relevel(.$rn, estados),
                                    cn = forcats::fct_relevel(.$cn, estados)) %>%
                              mutate(value = ifelse(.$rn %in% estados[x] | .$cn %in% estados[x], value, 0)) 

                    tabla %>% 
                     ggplot(aes(axis1 = rn, 
                                 axis2 = cn, 
                                  y = value),  # c("value", "freq", "tasa")
                             reverse = FALSE, 
                              na.rm = TRUE) + 
                      geom_alluvium(aes(fill = rn),  
                                     color = "transparent", 
                                      alpha = 0.8, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                       geom_stratum(aes(fill = rn), 
                                     color = "#F1F1F1", 
                                      alpha = 1, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                         geom_text_repel(aes(label = ifelse(after_stat(x)  == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                             fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = -.2, 
                                               force = 1,
                                                        force_pull = 0,
                                                         family = "montserrat",
                                                          reverse = FALSE) +
                          geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                              fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = .2, 
                                               force = 1,
                                                force_pull = 0,
                                                 family = "montserrat",
                                                  reverse = FALSE) +
                            theme_void() + 
                             theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                                    text = element_text(family = "montserrat"),
                                     axis.text = element_blank(),
                                      axis.title = element_blank(),
                                       strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                        legend.key.size = unit(0.5, "cm"),
                                         legend.text = element_text(size = 9, family = "montserrat"),
                                          legend.position = c(1, .5)) + 
                              scale_x_discrete(expand = c(-0.1, 0.35)) +
                               scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                                guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                                 labs(fill = "", 
                                      color = "")
              }
        )

path = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2010/GSankey de MR5a desagregado por estados_Absolutos.pdf")
ggexport(list = p, width = 14, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_ENT) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_ENT) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2010/Matriz de migracion reciente a nivel estatal 2005-2010.RData"))

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados
  
Residentes <- Migrantes %>%
               rownames_to_column() %>%
                tidyr::gather(CVE_ENT, Value, -rowname)%>%
                 filter(rowname == CVE_ENT) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                sna::diag.remove(remove.val = 0) %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "CVE_ENT") %>%
                   melt(., id.vars = "CVE_ENT", variable.name = "RES05EDO_C") %>%
                    mutate_at(vars(3), as.numeric) %>%
                     as_tibble() %>%
                      filter(CVE_ENT != RES05EDO_C) %>%
                       group_by(CVE_ENT) %>%
                        summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               sna::diag.remove(remove.val = 0) %>%
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_ENT") %>%
                  melt(., id.vars = "CVE_ENT", variable.name = "RES05EDO_C") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_ENT != RES05EDO_C) %>%
                      group_by(RES05EDO_C) %>%
                       summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                        rename("CVE_ENT" = "RES05EDO_C") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_ENT")) %>%
          left_join(., Residentes, by = c("CVE_ENT")) %>%
          left_join(., Inmigrantes, by = c("CVE_ENT")) %>%
          left_join(., Emigrantes, by = c("CVE_ENT")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2010/Indicadores de MR5a por estado 2005 - 2010.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Indicadores de MR5a por estado 2005 - 2010.RData"))
Indicadores de migración reciente
Nivel estatal
CVE_ENT Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001 1 178 800 1 057 013 989 821 48 334 28 166 20 168 76 500 8.6 5.0 3.6 −56 332
002 3 123 385 2 831 647 2 589 610 163 997 130 738 33 259 294 735 11.0 8.8 2.2 −261 476
003 633 854 574 700 482 904 80 371 22 354 58 017 102 725 26.6 7.4 19.2 −44 708
004 816 916 739 893 698 023 32 790 31 209 1 581 63 999 8.4 8.0 0.4 −62 418
005 2 739 691 2 476 193 2 380 518 66 820 63 448 3 372 130 268 5.1 4.9 0.3 −126 896
006 647 654 589 415 528 425 45 998 20 791 25 207 66 789 14.9 6.7 8.2 −41 582
007 4 788 162 4 229 610 4 142 076 55 796 102 327 −46 531 158 123 2.5 4.5 −2.1 −204 654
008 3 390 459 3 061 454 2 937 330 61 038 91 491 −30 453 152 529 3.8 5.7 −1.9 −182 982
009 8 783 909 8 169 822 7 753 531 329 942 702 870 −372 928 1 032 812 7.8 16.6 −8.8 −1 405 740
010 1 624 841 1 453 557 1 385 550 38 554 46 141 −7 587 84 695 5.0 6.0 −1.0 −92 282
011 5 474 270 4 925 229 4 733 141 98 285 73 885 24 400 172 170 3.8 2.8 0.9 −147 770
012 3 380 094 3 022 279 2 915 573 54 050 106 910 −52 860 160 960 3.4 6.7 −3.3 −213 820
013 2 674 391 2 412 330 2 238 160 128 120 79 816 48 304 207 936 10.1 6.3 3.8 −159 632
014 7 323 176 6 627 055 6 342 992 158 758 170 778 −12 020 329 536 4.6 4.9 −0.3 −341 556
015 15 123 304 13 687 763 12 956 936 605 173 458 002 147 171 1 063 175 8.4 6.4 2.0 −916 004
016 4 348 993 3 916 760 3 734 751 84 397 114 335 −29 938 198 732 4.1 5.5 −1.4 −228 670
017 1 769 804 1 616 490 1 500 899 82 574 51 711 30 863 134 285 9.8 6.1 3.6 −103 422
018 1 075 926 968 881 884 102 62 542 29 785 32 757 92 327 12.2 5.8 6.4 −59 570
019 4 641 903 4 212 743 3 989 017 140 665 81 009 59 656 221 674 6.4 3.7 2.7 −162 018
020 3 784 250 3 407 311 3 275 659 82 548 101 855 −19 307 184 403 4.6 5.7 −1.1 −203 710
021 5 778 539 5 197 320 5 004 644 129 835 146 232 −16 397 276 067 4.7 5.3 −0.6 −292 464
022 1 825 636 1 644 993 1 522 053 96 781 47 866 48 915 144 647 11.2 5.5 5.6 −95 732
023 1 319 485 1 183 406 1 023 772 139 679 57 413 82 266 197 092 22.3 9.2 13.1 −114 826
024 2 574 788 2 327 789 2 233 668 56 971 67 257 −10 286 124 228 4.6 5.5 −0.8 −134 514
025 2 760 401 2 501 472 2 392 883 75 218 100 479 −25 261 175 697 5.7 7.6 −1.9 −200 958
026 2 632 996 2 381 307 2 263 186 69 810 63 400 6 410 133 210 5.6 5.1 0.5 −126 800
027 2 236 189 2 012 666 1 940 421 57 816 72 642 −14 826 130 458 5.4 6.8 −1.4 −145 284
028 3 254 638 2 941 378 2 748 980 117 952 98 173 19 779 216 125 7.6 6.3 1.3 −196 346
029 1 180 714 1 062 720 1 011 697 40 737 25 260 15 477 65 997 7.3 4.5 2.8 −50 520
030 7 626 403 6 960 571 6 665 740 215 349 241 288 −25 939 456 637 5.9 6.6 −0.7 −482 576
031 1 952 423 1 778 938 1 707 091 51 915 36 777 15 138 88 692 5.6 3.9 1.6 −73 554
032 1 494 145 1 340 167 1 275 473 27 881 36 288 −8 407 64 169 3.9 5.1 −1.2 −72 576
Fuente: Estimaciones del CONAPO.

Nivel municipal

Migración reciente

Matrices

#Clave de los municipios 2010 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(RES05EDO_C, MUN05OTR_C)) %>%
               mutate(RES05EDO_C = case_when(.$RES05EDO_C %in% estados ~.$RES05EDO_C,
                                             .$RES05EDO_C %nin% estados ~ "888", #Residencia en otro país
                                             .$RES05EDO_C %in% "997" ~ "997",
                                             .$RES05EDO_C %in% "998" ~ "998",
                                             .$RES05EDO_C %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$RES05EDO_C %in% "997" ~ "997",
                                              .$RES05EDO_C %in% "998" ~ "998",
                                              .$RES05EDO_C %in% "999" ~ "999",
                                              .$MUN05OTR_C %in% "999" ~ "999")) %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset(EDAD >= 5 & EDAD <= 130) %>%
                   select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD) %>%
                    filter(CVE_MUN_RES %in% municipios) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/MC_municipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/MC_municipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan los nombres de los estados.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 

rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel municipal.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel municipal.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Reciente")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel municipal.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 2005 - 2010

Matriz de migración reciente 2005 - 2010
Nivel municipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 665374 597 552 0 762 121 124 17 98 17 269 104 235 182 359 0 0 0 0 70 0 0 0 0 0 0 0 0 0
001002 168 38598 0 0 0 12 0 0 0 15 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 76 0 44589 14 16 0 22 0 0 11 0 0 0 0 11 0 0 0 0 0 0 0 0 11 0 0 0 0 0
001004 50 0 0 12248 0 0 19 0 0 4 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 1875 22 0 0 77233 140 36 22 16 0 389 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 145 36 0 6 140 33542 96 38 20 18 234 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 36 45 18 54 45 75 40417 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 52 8 8 2 10 20 14 5934 0 0 2 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 90 10 0 25 0 53 258 10 16525 5 0 13 0 0 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 78 33 0 0 4 0 0 5 0 15235 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 2577 64 48 0 787 1068 204 22 80 0 24496 0 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 67 0 0 0 0 0 0 0 0 0 0 367600 3703 142 3105 641 97 403 1370 47 0 0 0 0 57 0 0 0 0
002002 903 0 0 0 0 0 0 0 0 0 0 1889 798622 761 2416 536 48 189 72 25 0 0 0 0 0 0 0 0 0
002003 50 0 0 0 0 0 11 0 0 0 0 222 654 74714 1642 64 0 45 27 6 0 0 0 0 0 0 0 0 0
002004 812 0 0 0 12 147 0 0 0 0 0 1905 2442 472 1248126 1414 0 0 956 100 0 0 0 0 0 0 0 0 0
002005 78 0 0 0 0 0 0 0 0 0 0 276 434 35 3920 66316 0 0 36 10 0 0 0 26 0 0 0 0 0
003001 48 0 0 0 0 0 0 0 0 0 0 163 70 0 282 34 57665 310 1220 736 66 0 0 0 0 0 0 0 0
003002 233 0 0 0 0 0 0 0 0 0 0 990 35 15 260 45 56 43931 408 50 71 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 603 449 0 854 0 2344 1108 204067 2520 391 0 0 0 0 0 0 0 0
003008 24 0 0 0 0 0 0 0 0 0 0 287 239 4 1374 56 260 13 1861 151222 180 0 0 0 0 0 0 0 0
003009 14 0 0 0 0 0 0 0 0 0 0 105 35 0 100 7 392 287 205 175 12240 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45660 0 11 11 33 11 11 0
004002 0 0 0 0 0 0 0 0 0 0 0 0 7 0 34 0 0 0 0 0 0 139 226429 277 285 146 94 13 6
004003 0 0 0 0 0 0 0 0 0 0 0 166 0 0 34 0 0 0 16 0 0 0 162 179398 66 0 0 226 0
004004 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 85 0 1024 67194 72 33 33 36
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 38 10 30 25 23321 0 10 15
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 14 14 42 21 32956 14 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 3 0 24 3 0 0 8250 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 15 38 14 9 37 9 10 8275
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 22 11 462 240 110 11 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

Gráficos por municipios

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel municipal.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)   

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE))

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#### Se anexa el filtro de estados filter(value > 100000000 & rn != estado[x]) %>% 
#### De esta manera solo se contemplan a los municipios

#p <- data.frame(estados = est,
 #               filtro_municipio = filtro_mig,
  #              filtro_estado = filtro_out)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel municipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                         tabla1[[i]] %>%
#                          as.data.frame() %>%
#                           adorn_totals(c("row", "col"), 
#                                         fill = "-", 
#                                          na.rm = TRUE, 
#                                           ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                            select(`Otros estados`) %>%
#                             slice(nrow(.)) %>%
#                              mutate(`Otros estados` = .$`Otros estados`/30) %>%
#                               pull(`Otros estados`)
#  }
#)

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                               tabla1[[i]] %>%
#                                as.data.frame() %>%
#                                 select(-c(`Otros estados`)) %>%
#                                  adorn_totals(c("row", "col"), 
#                                                fill = "-", 
#                                                 na.rm = TRUE, 
#                                                  ,,,,contains(colnames(tabla1[[i]])))  N%>% 
#                                    slice(nrow(.)) %>%
#                                     mutate(Total = .$Total/50) %>%
#                                      pull(Total)
#})

## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz MR5a nivel municipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel municipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel municipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")
# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz MR5a nivel municipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel municipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2010/ChordDiagram de MR5a desagregado por estado.pdf"

## Gráficos a nivel municipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file =  "/Graficos/Municipio/01_Migracion reciente/2010/Etiquetas a nivel municipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 10, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel municipal.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>% 
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
            mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                   Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                   Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                   Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                   Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                   Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Indicadores de migracion reciente a nivel municipal 2005 - 2010.xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Indicadores de migracion reciente a nivel municipal 2005 - 2010.RData"))
Indicadores de migración reciente
Nivel municipal
Clave de Entidad-Municipio Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 793 997 718 680 665 374 39 438 26 908 12 530 66 346 10.4 7.1 3.3 −53 816
001002 45 951 40 347 38 598 721 1 381 −660 2 102 3.3 6.4 −3.1 −2 762
001003 53 142 47 809 44 589 884 1 454 −570 2 338 3.5 5.8 −2.3 −2 908
001004 14 302 12 759 12 248 203 266 −63 469 3.0 3.9 −0.9 −532
001005 100 150 87 268 77 233 6 005 2 733 3 272 8 738 12.8 5.8 7.0 −5 466
001006 40 480 35 933 33 542 1 681 1 868 −187 3 549 8.8 9.8 −1.0 −3 736
001007 48 462 42 246 40 417 575 1 269 −694 1 844 2.5 5.6 −3.1 −2 538
001008 7 164 6 374 5 934 182 133 49 315 5.4 3.9 1.4 −266
001009 20 048 17 674 16 525 700 263 437 963 7.4 2.8 4.6 −526
001010 18 282 16 022 15 235 313 812 −499 1 125 3.6 9.5 −5.8 −1 624
001011 36 822 31 901 24 496 6 348 1 208 5 140 7 556 36.9 7.0 29.9 −2 416
002001 460 793 419 052 367 600 37 646 20 037 17 609 57 683 17.1 9.1 8.0 −40 074
002002 932 001 852 568 798 622 34 217 29 286 4 931 63 503 7.7 6.6 1.1 −58 572
002003 95 638 86 305 74 714 7 838 4 977 2 861 12 815 17.2 10.9 6.3 −9 954
002004 1 543 644 1 391 497 1 248 126 90 073 90 788 −715 180 861 12.3 12.4 −0.1 −181 576
002005 91 309 82 225 66 316 10 427 5 647 4 780 16 074 24.0 13.0 11.0 −11 294
003001 70 358 63 992 57 665 5 234 3 685 1 549 8 919 15.6 11.0 4.6 −7 370
003002 58 624 52 519 43 931 7 587 3 771 3 816 11 358 27.3 13.6 13.7 −7 542
003003 249 303 229 448 204 067 23 278 12 977 10 301 36 255 19.4 10.8 8.6 −25 954
003008 238 498 213 476 151 222 51 192 13 125 38 067 64 317 45.3 11.6 33.7 −26 250
Fuente: Estimaciones del CONAPO.

Migración Intramunicipal

Matrices

#Clave de los municipios 2010 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(RES05EDO_C, MUN05OTR_C)) %>%
               mutate(RES05EDO_C = case_when(.$RES05EDO_C %in% estados ~.$RES05EDO_C,
                                             .$RES05EDO_C %nin% estados ~ "888", #Residencia en otro país
                                             .$RES05EDO_C %in% "997" ~ "997",
                                             .$RES05EDO_C %in% "998" ~ "998",
                                             .$RES05EDO_C %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$RES05EDO_C %in% "997" ~ "997",
                                              .$RES05EDO_C %in% "998" ~ "998",
                                              .$RES05EDO_C %in% "999" ~ "999",
                                              .$MUN05OTR_C %in% "999" ~ "999")) %>%
                 mutate(I_Migracion = case_when(.$CVE_ENT == .$RES05EDO_C & .$RES05EDO_C %in% estados ~ 1,
                                                .$CVE_ENT != .$RES05EDO_C & .$RES05EDO_C %in% estados ~ 2,
                                                .$RES05EDO_C %nin% estados ~ 3)) %>%
                  mutate(EDAD = as.numeric(.$EDAD)) %>%
                   subset(EDAD >= 5 & EDAD <= 130) %>%
                    select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD, I_Migracion) %>%
                     filter(CVE_MUN_RES %in% municipios & I_Migracion == 1) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/MC_intramunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/MC_intramunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intramunicipal 2010.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intramunicipal 2010.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intramunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intramunicipal 2010.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 2005 - 2010

Matriz de migración reciente a nivel municipal
Nivel intramunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 665374 597 552 0 762 121 124 17 98 17 269 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001002 168 38598 0 0 0 12 0 0 0 15 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 76 0 44589 14 16 0 22 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 50 0 0 12248 0 0 19 0 0 4 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 1875 22 0 0 77233 140 36 22 16 0 389 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 145 36 0 6 140 33542 96 38 20 18 234 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 36 45 18 54 45 75 40417 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 52 8 8 2 10 20 14 5934 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 90 10 0 25 0 53 258 10 16525 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 78 33 0 0 4 0 0 5 0 15235 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 2577 64 48 0 787 1068 204 22 80 0 24496 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 367600 3703 142 3105 641 0 0 0 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 1889 798622 761 2416 536 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 222 654 74714 1642 64 0 0 0 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 1905 2442 472 1248126 1414 0 0 0 0 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 276 434 35 3920 66316 0 0 0 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 57665 310 1220 736 66 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 56 43931 408 50 71 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2344 1108 204067 2520 391 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 260 13 1861 151222 180 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 392 287 205 175 12240 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45660 0 11 11 33 11 11 0
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 139 226429 277 285 146 94 13 6
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 162 179398 66 0 0 226 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 85 0 1024 67194 72 33 33 36
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 38 10 30 25 23321 0 10 15
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 14 14 42 21 32956 14 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 24 3 0 0 8250 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 38 14 9 37 9 10 8275
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22 11 462 240 110 11 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intramunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)   

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#p <- data.frame(estados = est,
 #               filtro_municipio = tabla_municipios)
#write.table(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel intramunicipal.txt"), col.names = TRUE)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel intramunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel intramunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = NULL,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = NULL,
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = NULL)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                                  tabla1[[i]] %>%
#                                   as.data.frame() %>%
#                                    adorn_totals(c("row", "col"), 
#                                                  fill = "-", 
#                                                   na.rm = TRUE, 
#                                                    ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    select(Total) %>%
#                                     slice(nrow(.)) %>%
#                                      mutate(Total = .$Total/100) %>%
#                                       pull(Total)
#}
#)

## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz MR5a nivel intramunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}
 
saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel intramunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel intramunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz MR5a nivel intramunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel intramunicipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2010/ChordDiagram de MR5a desagregado a nivel intramunicipal.pdf"

## Gráficos a nivel intramunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/01_Migracion reciente/2010/Etiquetas a nivel intramunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intramunicipal 2010.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 5000) %>% 
               pull(rn)
################################################################################

tabla1 <- lapply(1:32, function(x){
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ substr(.$rn, 1, 3)),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ substr(.$cn, 1, 3))) %>%

                     mutate(value = ifelse(.$rn != .$cn, .$value, 0)) %>%
                      filter(value > 0) 
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/01_Migracion reciente/2010/GSankey de MR5a desagregado a nivel intramunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intramunicipal 2010.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Indicadores de MR5a a nivel intramunicipal 2010.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Indicadores de MR5a a nivel intramunicipal 2010.RData"))
Indicadores de migración reciente
Nivel intramunicipal
Clave de Entidad-Municipio Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 793 997 718 680 665 374 2 557 5 147 −2 590 7 704 0.68 1.36 −0.7 −10 294
001002 45 951 40 347 38 598 204 815 −611 1 019 0.95 3.78 −2.8 −1 630
001003 53 142 47 809 44 589 139 626 −487 765 0.55 2.48 −1.9 −1 252
001004 14 302 12 759 12 248 85 101 −16 186 1.26 1.49 −0.2 −202
001005 100 150 87 268 77 233 2 500 1 764 736 4 264 5.34 3.76 1.6 −3 528
001006 40 480 35 933 33 542 733 1 489 −756 2 222 3.84 7.79 −4.0 −2 978
001007 48 462 42 246 40 417 291 773 −482 1 064 1.28 3.41 −2.1 −1 546
001008 7 164 6 374 5 934 116 114 2 230 3.43 3.37 0.1 −228
001009 20 048 17 674 16 525 451 214 237 665 4.78 2.27 2.5 −428
001010 18 282 16 022 15 235 120 70 50 190 1.40 0.82 0.6 −140
001011 36 822 31 901 24 496 4 850 933 3 917 5 783 28.23 5.43 22.8 −1 866
002001 460 793 419 052 367 600 7 591 4 292 3 299 11 883 3.45 1.95 1.5 −8 584
002002 932 001 852 568 798 622 5 602 7 233 −1 631 12 835 1.26 1.62 −0.4 −14 466
002003 95 638 86 305 74 714 2 582 1 410 1 172 3 992 5.68 3.10 2.6 −2 820
002004 1 543 644 1 391 497 1 248 126 6 233 11 083 −4 850 17 316 0.85 1.51 −0.7 −22 166
002005 91 309 82 225 66 316 4 665 2 655 2 010 7 320 10.75 6.12 4.6 −5 310
003001 70 358 63 992 57 665 2 332 3 052 −720 5 384 6.94 9.09 −2.1 −6 104
003002 58 624 52 519 43 931 585 1 718 −1 133 2 303 2.11 6.18 −4.1 −3 436
003003 249 303 229 448 204 067 6 363 3 694 2 669 10 057 5.32 3.09 2.2 −7 388
003008 238 498 213 476 151 222 2 314 3 481 −1 167 5 795 2.05 3.08 −1.0 −6 962
Fuente: Estimaciones del CONAPO.

Migración Intermunicipal

Matrices

#Clave de los municipios 2010 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(RES05EDO_C, MUN05OTR_C)) %>%
               mutate(RES05EDO_C = case_when(.$RES05EDO_C %in% estados ~.$RES05EDO_C,
                                             .$RES05EDO_C %nin% estados ~ "888", #Residencia en otro país
                                             .$RES05EDO_C %in% "997" ~ "997",
                                             .$RES05EDO_C %in% "998" ~ "998",
                                             .$RES05EDO_C %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$RES05EDO_C %in% "997" ~ "997",
                                              .$RES05EDO_C %in% "998" ~ "998",
                                              .$RES05EDO_C %in% "999" ~ "999",
                                              .$MUN05OTR_C %in% "999" ~ "999")) %>%
                 mutate(I_Migracion = case_when(.$CVE_ENT == .$RES05EDO_C & .$RES05EDO_C %in% estados ~ 1,
                                                .$CVE_ENT != .$RES05EDO_C & .$RES05EDO_C %in% estados ~ 2,
                                                .$RES05EDO_C %nin% estados ~ 3)) %>%
                  mutate(EDAD = as.numeric(.$EDAD)) %>%
                   subset(EDAD >= 5 & EDAD <= 130) %>%
                    select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD, I_Migracion) %>%
                     filter(CVE_MUN_RES %in% municipios & I_Migracion == 2) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/MC_intermunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/MC_intermunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intermunicipal 2010.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intermunicipal 2010.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intermunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intermunicipal 2010.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 2005 - 2010

Matriz de migración reciente a nivel municipal
Nivel intermunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 0 0 0 0 0 0 0 0 0 0 0 104 235 182 359 0 0 0 0 70 0 0 0 0 0 0 0 0 0
001002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 11 0 0 0 0 0
001004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 0 0 0 0 0 0 0 0 0 0 0 13 0 0 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 0 0 0 0 0 0 0 0 0 0 0 0 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 67 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97 403 1370 47 0 0 0 0 57 0 0 0 0
002002 903 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 189 72 25 0 0 0 0 0 0 0 0 0
002003 50 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 45 27 6 0 0 0 0 0 0 0 0 0
002004 812 0 0 0 12 147 0 0 0 0 0 0 0 0 0 0 0 0 956 100 0 0 0 0 0 0 0 0 0
002005 78 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 36 10 0 0 0 26 0 0 0 0 0
003001 48 0 0 0 0 0 0 0 0 0 0 163 70 0 282 34 0 0 0 0 0 0 0 0 0 0 0 0 0
003002 233 0 0 0 0 0 0 0 0 0 0 990 35 15 260 45 0 0 0 0 0 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 603 449 0 854 0 0 0 0 0 0 0 0 0 0 0 0 0 0
003008 24 0 0 0 0 0 0 0 0 0 0 287 239 4 1374 56 0 0 0 0 0 0 0 0 0 0 0 0 0
003009 14 0 0 0 0 0 0 0 0 0 0 105 35 0 100 7 0 0 0 0 0 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004002 0 0 0 0 0 0 0 0 0 0 0 0 7 0 34 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004003 0 0 0 0 0 0 0 0 0 0 0 166 0 0 34 0 0 0 16 0 0 0 0 0 0 0 0 0 0
004004 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intermunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)
             
################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value > 0)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#p <- data.frame(estados = est,
 #               filtro_municipio = tabla_municipios,
  #              filtro_estado = tabla_estados)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel intermunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2010/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                                 tabla1[[i]] %>%
#                                  as.data.frame() %>%
#                                   adorn_totals(c("row", "col"), 
#                                                 fill = "-", 
#                                                  na.rm = TRUE, 
#                                                   ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    select(`Otros estados`) %>%
#                                     slice(nrow(.)) %>%
#                                      mutate(`Otros estados` = .$`Otros estados`/10) %>%
#                                       pull(`Otros estados`)
#})

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                               tabla1[[i]] %>%
#                                as.data.frame() %>%
#                                 select(-c(`Otros estados`)) %>%
#                                  adorn_totals(c("row", "col"), 
#                                                fill = "-", 
#                                                 na.rm = TRUE, 
#                                                  ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    slice(nrow(.)) %>%
#                                     mutate(Total = .$Total/50) %>%
#                                      pull(Total)
#})
## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz MR5a a nivel intermunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel intermunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel intermunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz MR5a a nivel intermunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Tabla MR5a a nivel intermunicipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2010/ChordDiagram de MR5a desagregado a nivel intermunicipal.pdf"

## Gráficos a nivel intermunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/01_Migracion reciente/2010/Etiquetas a nivel intermunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intermunicipal 2010.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 5000) %>% 
               pull(rn)
################################################################################

tabla1 <-  lapply(1:32, function(x){
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0(estados[x], " Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ paste0(nom_estados[as.numeric(substr(.$rn, 1, 3))])),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0(estados[x], " Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ paste0(nom_estados[as.numeric(substr(.$cn, 1, 3))]))) %>%
                     mutate(value = ifelse(.$rn != .$cn & (substr(.$rn, 1, 3) %in% estados[x] | substr(.$cn, 1, 3) %in% estados[x]), .$value, 0)) %>%
                      filter(value > 0)  
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/01_Migracion reciente/2010/GSankey de MR5a desagregado a nivel intermunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Matriz de migracion reciente a nivel intermunicipal 2010.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Indicadores de MR5a a nivel intermunicipal 2010.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2010/Indicadores de MR5a a nivel intermunicipal 2010.RData"))
Indicadores de migración reciente
Nivel intermunicipal
Clave de Entidad-Municipio Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 793 997 718 680 0 36 881 21 761 15 120 58 642 9.8 5.75 4.0 −43 522
001002 45 951 40 347 0 517 566 −49 1 083 2.4 2.62 −0.2 −1 132
001003 53 142 47 809 0 745 828 −83 1 573 3.0 3.28 −0.3 −1 656
001004 14 302 12 759 0 118 165 −47 283 1.7 2.44 −0.7 −330
001005 100 150 87 268 0 3 505 969 2 536 4 474 7.5 2.07 5.4 −1 938
001006 40 480 35 933 0 948 379 569 1 327 5.0 1.98 3.0 −758
001007 48 462 42 246 0 284 496 −212 780 1.3 2.19 −0.9 −992
001008 7 164 6 374 0 66 19 47 85 2.0 0.56 1.4 −38
001009 20 048 17 674 0 249 49 200 298 2.6 0.52 2.1 −98
001010 18 282 16 022 0 193 742 −549 935 2.3 8.65 −6.4 −1 484
001011 36 822 31 901 0 1 498 275 1 223 1 773 8.7 1.60 7.1 −550
002001 460 793 419 052 0 30 055 15 745 14 310 45 800 13.7 7.16 6.5 −31 490
002002 932 001 852 568 0 28 615 22 053 6 562 50 668 6.4 4.94 1.5 −44 106
002003 95 638 86 305 0 5 256 3 567 1 689 8 823 11.6 7.84 3.7 −7 134
002004 1 543 644 1 391 497 0 83 840 79 705 4 135 163 545 11.4 10.86 0.6 −159 410
002005 91 309 82 225 0 5 762 2 992 2 770 8 754 13.3 6.90 6.4 −5 984
003001 70 358 63 992 0 2 902 633 2 269 3 535 8.6 1.88 6.8 −1 266
003002 58 624 52 519 0 7 002 2 053 4 949 9 055 25.2 7.39 17.8 −4 106
003003 249 303 229 448 0 16 915 9 283 7 632 26 198 14.1 7.76 6.4 −18 566
003008 238 498 213 476 0 48 878 9 644 39 234 58 522 43.3 8.54 34.7 −19 288
Fuente: Estimaciones del CONAPO.

Referencias

Librerias que se usaron en el documento

package loadedversion source
Cairo 1.6-1 CRAN (R 4.3.1)
chorddiag 0.1.3 Github ()
circlize 0.4.15 CRAN (R 4.3.1)
doMC 1.3.5 R-Forge (R 4.3.1)
dplyr 1.1.3 CRAN (R 4.3.2)
expss 0.11.6 CRAN (R 4.3.1)
foreach 1.5.2 CRAN (R 4.3.1)
ggalluvial 0.12.5 CRAN (R 4.3.1)
ggplot2 3.4.3 CRAN (R 4.3.1)
ggpubr 0.6.0 CRAN (R 4.3.1)
ggrepel 0.9.3 CRAN (R 4.3.1)
ggsankey 0.0.99999 Github ()
gt 0.10.0 CRAN (R 4.3.1)
haven 2.5.3 CRAN (R 4.3.1)
Hmisc 5.1-0 CRAN (R 4.3.1)
iterators 1.0.14 CRAN (R 4.3.1)
janitor 2.2.0 CRAN (R 4.3.1)
kableExtra 1.3.4 CRAN (R 4.3.1)
knitr 1.45 CRAN (R 4.3.2)
maditr 0.8.3 CRAN (R 4.3.1)
mapview 2.11.0 CRAN (R 4.3.1)
Matrix 1.6-1.1 CRAN (R 4.3.1)
network 1.18.1 CRAN (R 4.3.1)
openxlsx 4.2.5.2 CRAN (R 4.3.1)
reshape2 1.4.4 CRAN (R 4.3.1)
sjlabelled 1.2.0 CRAN (R 4.3.1)
sna 2.7-1 CRAN (R 4.3.1)
srvyr 1.2.0 CRAN (R 4.3.1)
statnet.common 4.9.0 CRAN (R 4.3.1)
stringr 1.5.0 CRAN (R 4.3.1)
survey 4.2 Github ()
survival 3.5-5 CRAN (R 4.3.1)
tibble 3.2.1 CRAN (R 4.3.1)
tidyr 1.3.0 CRAN (R 4.3.1)

Creative Commons Licence
This work by Diana Villasana Ocampo is licensed under a Creative Commons Attribution 4.0 International License.